package springmvc.common;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class CustomDateSerializer extends JsonDeserializer<Date> {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {

        String date = jp.getText();
        try {
            if(date!=null&&date.length()>0 ) {
                return this.format.parse(date);
            }
            return new Date();
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

}